package com.book.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.book.domain.Book;
import com.book.domain.User;
import com.book.util.DBUtil;

public class BookDao {

	public List<Book> listAllBooks(){
		List<Book> bookslist=new ArrayList<Book>();
	Connection connection=	DBUtil.getConnection();
	PreparedStatement preparedStatement=null;
	ResultSet resultSet=null;
	String sql="select * from t_book ";
	try {
		preparedStatement=	connection.prepareStatement(sql);
	    resultSet=	preparedStatement.executeQuery();
	    while (resultSet.next()) {
			Book book=new Book(resultSet.getInt("id"), resultSet.getString("bookname"), resultSet.getString("authorname"), resultSet.getString("publishername"), resultSet.getString("bookcatalog"), resultSet.getString("bookimgurl"), resultSet.getString("bookoutline"));
			bookslist.add(book);
			
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}finally{
		
		DBUtil.closeConnection(preparedStatement, resultSet, connection);
	}
		
		return bookslist;
		
	}
	public void addBook(Book book){
		Connection connection=	DBUtil.getConnection();
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		String sql="insert into t_book(bookname,authorname,publishername,bookcatalog,bookimgurl,bookoutline) values(?,?,?,?,?,?) ";
	try {
		preparedStatement=	connection.prepareStatement(sql);
		preparedStatement.setString(1, book.getBookname());
		preparedStatement.setString(2, book.getAuthorname());
		preparedStatement.setString(3, book.getPublishername());
		preparedStatement.setString(4, book.getBookcatalog());
		preparedStatement.setString(5, book.getBookimgurl());
		preparedStatement.setString(6, book.getBookoutline());
		preparedStatement.executeUpdate();
		System.out.println("插入记录成功");
	} catch (SQLException e) {
		e.printStackTrace();
	}finally{
		DBUtil.closeConnection(preparedStatement, resultSet, connection);
		
	}
		
		
	}
	
}
